//
//  ResetPwdViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/11.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "ResetPwdViewController.h"
#import "AESEnrypt.h"
#import "NSString+AES.h"
#import "CountryCodeViewController.h"

@interface ResetPwdViewController ()<UITextFieldDelegate>
{
//    UIButton *countryCodeBtn;
    UITextField *phoneTextField;
    UITextField *captchaTextField;
    UITextField *passwordTextField;
    
    UIButton *captchaButton;
    NSInteger timerInt;
    
    NSString *countryCodeStr;
}
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ResetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    countryCodeStr = @"86";
    countryCodeStr = @"";
    timerInt = CaptchaTime;
    [self initUI];
}
- (void)countryCodeBtnTap {
    CountryCodeViewController *CCVC = [[CountryCodeViewController alloc] init];
    CCVC.selectCode = ^(NSString * _Nonnull code) {
        self->countryCodeStr = code;
//        [self->countryCodeBtn setTitle:[NSString stringWithFormat:@"+ %@", code] forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:CCVC animated:YES];
}
- (void)initUI {
    self.navigationItem.title = @"设置新密码";
    
//    countryCodeBtn = [UIToolsControl initButtonWithFrame:CGRectMake(ScalePS(20), ScalePS(25), ScalePS(50), ScalePS(50)) imageName:nil target:self action:@selector(countryCodeBtnTap) title:[NSString stringWithFormat:@"+ %@", countryCodeStr] titleColor:THEMECOLOR_OF_TEXT_333333];
//    countryCodeBtn.titleLabel.font = Font(Font_15);
//    countryCodeBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
//    countryCodeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    [self.view addSubview:countryCodeBtn];
    
    phoneTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(30*Scale, 25*Scale, WIDTH-60*Scale, 50*Scale) placeholder:PhoneNumberPlaceholderText secureTextEntry:NO font:Font_17 padding:YES];
    [phoneTextField borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    phoneTextField.delegate = self;
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phoneTextField];
    if (self.phoneNumber && self.phoneNumber.length) {
        phoneTextField.placeholder = self.phoneNumber;
        phoneTextField.userInteractionEnabled = NO;
    }
    
    captchaTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(30*Scale, EndY(phoneTextField)+25*Scale, WIDTH-150*Scale, 50*Scale) placeholder:CaptchaPlaceholderText secureTextEntry:NO font:Font_17 padding:YES];
    captchaTextField.delegate = self;
    captchaTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:captchaTextField];
    UIView *lineView = [UIToolsControl initViewWithFrame:CGRectMake(30*Scale, EndY(captchaTextField)-1, WIDTH-60*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
    [self.view addSubview:lineView];
    captchaButton = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-120*Scale, EndY(phoneTextField)+30*Scale, 100*Scale, 40*Scale) imageName:nil target:self action:@selector(sendCaptchaBtnTap) title:@"发送验证码" titleColor:THEMECOLOR_OF_TEXT_333333];
    captchaButton.titleLabel.font = Font(Font_14);
    [self.view addSubview:captchaButton];
    
    passwordTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(30*Scale, EndY(captchaTextField)+25*Scale, WIDTH-60*Scale, 50*Scale) placeholder:NewPasswordPlaceholderText secureTextEntry:YES font:Font_17 padding:YES];
    [passwordTextField borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    passwordTextField.delegate = self;
    passwordTextField.keyboardType = UIKeyboardTypeNamePhonePad;
    [self.view addSubview:passwordTextField];
    
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(40*Scale, EndY(passwordTextField)+60*Scale, WIDTH-80*Scale, 46*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"完成" titleColor:COLOR_OF_WHITE];
    okBtn.titleLabel.font = FontBold(Font_17);
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.layer.cornerRadius = 23*Scale;
    okBtn.layer.masksToBounds = YES;
    [self.view addSubview:okBtn];
}
- (void)okBtnTap {
    if (captchaTextField.text.length>0) {
        if ([UIToolsControl validateNumber:captchaTextField.text]) {
            if (passwordTextField.text.length>=6) {
                [self upDate];
            } else {
                SVPHUDError(PasswordAlertText);
                [passwordTextField becomeFirstResponder];
            }
        } else {
            SVPHUDError(CaptchaOKAlertText);
            [captchaTextField becomeFirstResponder];
        }
    } else {
        SVPHUDError(CaptchaAlertText);
        [captchaTextField becomeFirstResponder];
    }
}
- (void)upDate {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:((self.phoneNumber && self.phoneNumber.length)?self.phoneNumber:phoneTextField.text) forKey:@"mobile"];
    [dic setObject:captchaTextField.text forKey:@"verify_code"];
    [dic setObject:[passwordTextField.text aci_encryptWithAES] forKey:@"new_password"];
    [dic setObject:countryCodeStr forKey:@"country_code"];
    
    [BaseNetManager requestWithMethod:POST path:ResetPassword_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {
            NSString *str = [NSString stringWithFormat:@"%@", responseObject[@"msg"]];
            SVPHUDError(str);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)sendCaptchaBtnTap {
    NSLog(@"sendCaptchaBtnTap");
    SVPHUDShow;
    __weak __typeof(self)weakSelf = self;
    [UIToolsControl getCaptcha:((self.phoneNumber && self.phoneNumber.length)?self.phoneNumber:phoneTextField.text) countryCode:countryCodeStr type:[UIToolsControl getCaptchaType:GetCaptchaTypeOfChangePassword] successBlock:^(id response) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if ([response[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(response[@"msg"]);
            [self->captchaTextField becomeFirstResponder];
            strongSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
        } else {
            SVPHUDError(response[@"msg"]);
        }
    }];
}
- (void)timerClick {
    timerInt--;
    if (timerInt>0) {
        [captchaButton setTitleColor:THEMECOLOR_OF_TEXT_cccccc forState:UIControlStateNormal];
        [captchaButton setTitle:[NSString stringWithFormat:@"%ldS", (long)timerInt] forState:UIControlStateNormal];
        captchaButton.userInteractionEnabled = NO;
    } else {
        [captchaButton setTitle:GetCaptchaText forState:UIControlStateNormal];
        [captchaButton setTitleColor:THEMECOLOR_OF_TEXT_333333 forState:UIControlStateNormal];
        captchaButton.userInteractionEnabled = YES;
        timerInt = CaptchaTime;
        [self.timer invalidate];
        self.timer = nil;
    }
}

@end
